import { createRouter, createWebHistory } from "vue-router";
import { useSideStore } from '@/store/side'
import { getToken } from '@/utils/auth'
const router = createRouter({
  history: createWebHistory('/pcadmin/'),
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      component: () => import("@/views/login/index.vue"),
    },
    {
      path: "/admin",
      component: () => import("@/views/layout/layout.vue"),
      redirect: "/admin/user",
      children:[
        {
          path: "user",
          component: () => import("@/views/user/user.vue"),
        },
        {
          path: "task",
          component: () => import("@/views/task/index.vue"),
        },
        {
          path: "addtask",
          component: () => import("@/views/task/add.vue"),
        },
        {
          path: "taskdetail",
          component: () => import("@/views/task/detail.vue"),
        }, 
        {
          path: "level",
          component: () => import("@/views/pagelevel/index.vue"),
        },
        {
          path: "leveldetail",
          component: () => import("@/views/pagelevel/detail.vue"),
        },
        {
          path: "manager",
          component: () => import("@/views/manager/index.vue"),
        }, 
      ]
    }
  ]
});


router.afterEach((to, from) => {
  const menuStore = useSideStore()
  menuStore.setMenuItem(to)
  if (to.fullPath != '/login') {
    if (!getToken()) {
      router.push('/login')
    }
  }
});

export default router;